file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/packages)
if (BUILD_PRERELEASE)
    set(FDB_DOC_PKG_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
    set(PACKAGING_RELEASE_FLAG 0PRERELEASE)
    set(PKG_FILE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/packages/pkgs/FoundationDB-Document-Layer-${FDB_DOC_PKG_VERSION}-PRERELEASE.pkg)
else()
    set(FDB_DOC_PKG_VERSION ${PROJECT_VERSION})
    set(PACKAGING_RELEASE_FLAG 1)
    set(PKG_FILE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/packages/pkgs/FoundationDB-Document-Layer-${FDB_DOC_PKG_VERSION}.pkg)
endif()
set(RPM_FILES_OUTPUT_PATH ${CMAKE_BINARY_DIR}/packages/rpms/)
set(DEB_FILES_OUTPUT_PATH ${CMAKE_BINARY_DIR}/packages/debs/)

if (APPLE)
    file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/packages/pkgs/)
    set(PKG_PACKAGING_SCRIPT_PATH ${CMAKE_SOURCE_DIR}/packaging/osx/buildpkg.sh)
    set(PKG_PACKAGING_SCRIPT_PARAMS ${PKG_FILE_OUTPUT_PATH} ${FDB_DOC_PKG_VERSION} ${PACKAGING_RELEASE_FLAG} ${CMAKE_BINARY_DIR}/bin/fdbdoc ${FdbMonitor_EXECUTABLE_PATH})
    add_custom_command(TARGET fdbdoc
        POST_BUILD
        COMMAND ${PKG_PACKAGING_SCRIPT_PATH} ${PKG_PACKAGING_SCRIPT_PARAMS}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )
else()
    # build RPM packages
    file(MAKE_DIRECTORY ${RPM_FILES_OUTPUT_PATH})
    file(MAKE_DIRECTORY ${DEB_FILES_OUTPUT_PATH})
    set(RPM_PACKAGING_SCRIPT_PATH ${CMAKE_SOURCE_DIR}/packaging/rpm/buildrpms.sh)
    set(RPM_PACKAGING_SCRIPT_PARAMS ${RPM_FILES_OUTPUT_PATH} ${FDB_DOC_PKG_VERSION} ${PACKAGING_RELEASE_FLAG} ${CMAKE_BINARY_DIR}/bin/fdbdoc ${FdbMonitor_EXECUTABLE_PATH})
    add_custom_command(TARGET fdbdoc
        POST_BUILD
        COMMAND ${RPM_PACKAGING_SCRIPT_PATH} ${RPM_PACKAGING_SCRIPT_PARAMS}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )
    # build DEB packages
    set(DEB_PACKAGING_SCRIPT_PATH ${CMAKE_SOURCE_DIR}/packaging/deb/builddebs.sh)
    set(DEB_PACKAGING_SCRIPT_PARAMS ${DEB_FILES_OUTPUT_PATH} ${FDB_DOC_PKG_VERSION} ${PACKAGING_RELEASE_FLAG} ${CMAKE_BINARY_DIR}/bin/fdbdoc ${FdbMonitor_EXECUTABLE_PATH})
    add_custom_command(TARGET fdbdoc
        POST_BUILD
        COMMAND ${DEB_PACKAGING_SCRIPT_PATH} ${DEB_PACKAGING_SCRIPT_PARAMS}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )
endif()
